package com.macro.cloud.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.macro.cloud.config.Oauth2Config;
import com.macro.cloud.domain.AccessTokenDto;
import com.macro.cloud.domain.Oauth2ClientUserDto;
import com.macro.cloud.domain.UserDto;
import com.macro.cloud.utils.ServletUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;

public class Oauth2ClientService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private Oauth2Config oauth2Config;

    public Oauth2ClientService(Oauth2Config oauth2Config) {
        this.oauth2Config = oauth2Config;
        logger.info("oauth2 config info:");
        logger.info("getOauth2Server:{}",oauth2Config.getOauth2Server());
        logger.info("getClientId:{}",oauth2Config.getClientId());
        logger.info("getClientSecret:{}",oauth2Config.getClientSecret());
        logger.info("getOauth2CodeUrl:{}",oauth2Config.getOauth2CodeUrl());
        logger.info("getJwtSigningKey:{}",oauth2Config.getJwtSigningKey());
    }

    /**
     * 获取授权码链接
     * @return
     */
    public String getOauth2CodeUrl() {
        return oauth2Config.getOauth2CodeUrl();
    }

    /**
     * 根据授权码获取token信息
     * @param code
     * @return
     */
    public AccessTokenDto getAccessToken(String code){
        // 封装授权信息
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("grant_type","authorization_code");
        paramMap.put("code", code);
        paramMap.put("client_id",oauth2Config.getClientId());
        paramMap.put("redirect_uri",oauth2Config.getRedirectUri());
        paramMap.put("scope","all");
        // 获取token
        String access_token = HttpRequest.post(oauth2Config.getOauth2TokenApi())
                .basicAuth(oauth2Config.getClientId(), oauth2Config.getClientSecret())
                .contentType("application/x-www-form-urlencoded")
                .form(paramMap)
                .execute().body();
        AccessTokenDto accessTokenDto = JSON.parseObject(access_token, AccessTokenDto.class);
        return accessTokenDto;
    }

    /**
     * 获取oauth2用户信息
     * @param accessTokenDto
     * @return
     */
    public Oauth2ClientUserDto getCurrentUser(AccessTokenDto accessTokenDto){
        String loginUserJson = HttpRequest.get(oauth2Config.getOauth2CurrentUserApi())
                .header("Authorization", "bearer " + accessTokenDto.getAccess_token())
                .execute().body();
        Oauth2ClientUserDto oauth2ClientUserDto = JSON.parseObject(loginUserJson, Oauth2ClientUserDto.class);
        return oauth2ClientUserDto;
    }


    public Object getUserInfo(String username) {
        String userInfoJson = HttpRequest.get(oauth2Config.getOauth2UserApi())
                .form("username",username)
                .execute().body();
        UserDto userDto = JSON.parseObject(userInfoJson, UserDto.class);
        return userDto;
    }



}
